﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;

public partial class admins_Product : System.Web.UI.Page
{
    static string Id = "";
    static bool Insert = false;
    static string Level = "";
    static string Lang = "";
    protected static string anh_nho="";
    protected static string anh_to="";
    protected static string anh_slide = "";
    public static string ListName = "";
    private static List<ObjLoaiTour> lstLoaiTour;
    private static List<ObjKhuyenMai> lstKhuyenMai;
    protected void Page_Load(object sender, EventArgs e)
    {
        //config ckeditor
        //cau hinh cke
        //cke_mo_ta_ngan.config.toolbar = new object[]
        //    {
        //        new object[] { "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
        //        new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
        //        new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
        //        new object[] { "BidiLtr", "BidiRtl" },
				
        //        new object[] { "Styles", "Format", "Font", "FontSize" },
        //    };
        
        CKFinder.FileBrowser a = new CKFinder.FileBrowser();
        a.SetupCKEditor(cke_noi_dung);

        drop_danh_muc_san_pham.SelectedIndexChanged += new EventHandler(drop_danh_muc_san_pham_SelectedIndexChanged);
        grid_san_pham.ItemCommand += new DataGridCommandEventHandler(grid_san_pham_ItemCommand);
        grid_san_pham.PageIndexChanged += new DataGridPageChangedEventHandler(grid_san_pham_PageIndexChanged);

        if (!IsPostBack)
        {
            LoaiTour loaiTour = new LoaiTour();
            lstLoaiTour = new List<ObjLoaiTour>();
            lstLoaiTour = loaiTour.getAllDanhMucTour();
            drop_danh_muc_san_pham.DataSource = lstLoaiTour;
            drop_danh_muc_san_pham.DataTextField = "ten_loai_tour";
            drop_danh_muc_san_pham.DataValueField = "loai_tour_id";
            drop_danh_muc_san_pham.DataBind();
            layTour();
        }
    }

    void grid_san_pham_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grid_san_pham.CurrentPageIndex = e.NewPageIndex;
        layTour();
    }




    void grid_san_pham_ItemCommand(object source, DataGridCommandEventArgs e)
    {

        string strCA = e.CommandArgument.ToString();
        Id = strCA;
        Tour Tour = new Tour();
        ObjTour objTour = new ObjTour();
        switch (e.CommandName)
        {
            case "Edit":
                Insert = false;
                pnView.Visible = false;
                pnUpdate.Visible = true;
                //lay danh sach tour
                drop_danh_muc.DataSource = lstLoaiTour;
                drop_danh_muc.DataTextField = "ten_loai_tour";
                drop_danh_muc.DataValueField = "loai_tour_id";
                drop_danh_muc.DataBind();
                //lay chi tiet khuyen mai
                KhuyenMai khuyenmai = new KhuyenMai();
                lstKhuyenMai = khuyenmai.getAllKhuyenMai();
                ObjKhuyenMai objkhuyenmai = new ObjKhuyenMai();
                Drop_khuyen_mai.DataSource = lstKhuyenMai;
                Drop_khuyen_mai.DataTextField = "Ten_khuyen_mai";
                Drop_khuyen_mai.DataValueField = "Ma_khuyen_mai";
                Drop_khuyen_mai.DataBind();
                

                //lay chi tiet tour
                objTour = Tour.getTourByID(strCA);
                txt_ten_tour.Text = objTour.Ten_tour;
                txt_ma_tour.Text = objTour.Ma_tour;
                cke_mo_ta_ngan.Text = objTour.Mo_ta_ngan;
               
                txt_gia_thi_truong.Text = objTour.Gia_thi_truong.ToString();
                txt_gia_khuyen_mai.Text = objTour.Gia_khuyen_mai.ToString();
                txt_so_ngay.Text = objTour.So_ngay.ToString();
                txt_so_dem.Text = objTour.So_dem.ToString();
                txt_vi_tri.Text = objTour.Vi_tri.ToString();
                txt_so_cho.Text = objTour.So_cho.ToString();
                txt_ngay_khoi_hanh.Text = objTour.Ngay_khoi_hanh;
                txt_ngay_ket_thuc.Text = objTour.Ngay_ket_thuc;
                cke_noi_dung.Text = objTour.Noi_dung;
                anh_nho = objTour.Anh_nho;
                anh_to = objTour.Anh_to;
                anh_slide = objTour.Anh_slide;
                if (objTour.Tour_ban_chay == 1)
                {
                    ck_tour_ban_chay.Checked = true;
                }
                if (objTour.Tour_noi_bat == 1)
                {
                    ck_tour_noi_bat.Checked = true;
                }
                ListItem selectedItem = new ListItem();
                selectedItem = drop_danh_muc.Items.FindByValue(objTour.Loai_tour_id.ToString());
                if (selectedItem != null)
                {
                    selectedItem.Selected = true;
                }
                ListItem selectedItem1 = new ListItem();
                selectedItem1 = Drop_khuyen_mai.Items.FindByValue(objTour.Ma_khuyen_mai.ToString());
                if (selectedItem1 != null)
                {
                    selectedItem1.Selected = true;
                }

                break;

            case "Delete":
                Insert = false;
                Tour.deleteTour(strCA);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                break;
        }
    }



    void drop_danh_muc_san_pham_SelectedIndexChanged(object sender, EventArgs e)
    {
        layTour();
    }

    void layTour()
    {
        Tour Tour = new Tour();
        List<ObjTour> lst = new List<ObjTour>();
        lst = Tour.getTourByLoaiTourId(drop_danh_muc_san_pham.SelectedValue, Constants.ALL);
        if (lst.Count > 0)
        {
            grid_san_pham.DataSource = lst;
            grid_san_pham.DataBind();
            lb_messenger.Text = "";
        }
        else
        {
            lb_messenger.Text = "Không có tour";
            grid_san_pham.DataSource = null;
            grid_san_pham.DataBind();
        }
    }





    protected void AddButton_Click(object sender, EventArgs e)
    {

        pnUpdate.Visible = true;
        pnView.Visible = false;
        Insert = true;
        //resert textbox
        KhuyenMai khuyenmai = new KhuyenMai();
        lstKhuyenMai = khuyenmai.getAllKhuyenMai();
        txt_ma_tour.Text = "";
        txt_ten_tour.Text = "";
        txt_gia_khuyen_mai.Text = "0";
        txt_gia_thi_truong.Text = "0";
        cke_mo_ta_ngan.Text = "";
        cke_noi_dung.Text = "";
        txt_so_ngay.Text = "0";
        txt_so_dem.Text = "0";
        txt_so_cho.Text = "0";
        txt_ngay_khoi_hanh.Text = "Liên Hệ";
        txt_ngay_ket_thuc.Text = "Liên Hệ";
        anh_nho = "";
        anh_to = "";
        anh_slide = "";
        LoaiTour loaiTour = new LoaiTour();
        drop_danh_muc.DataSource = lstLoaiTour;
        drop_danh_muc.DataTextField = "ten_loai_tour";
        drop_danh_muc.DataValueField = "loai_tour_id";
        drop_danh_muc.DataBind();

        //KhuyenMai khuyenmai = new KhuyenMai();
        //lstKhuyenMai = khuyenmai.getAllKhuyenMai();
        Drop_khuyen_mai.DataSource = lstKhuyenMai;
        Drop_khuyen_mai.DataTextField = "Ten_khuyen_mai";
        Drop_khuyen_mai.DataValueField = "Ma_khuyen_mai";
        Drop_khuyen_mai.DataBind();

    }

    protected void DeleteButton_Click(object sender, EventArgs e)
    {

        string sql = "";
        foreach (DataGridItem item in grid_san_pham.Items)
        {
            CheckBox checkBox = (CheckBox)item.Cells[0].Controls[1];

            if (checkBox.Checked == true)
            {
                sql += "<Tour Tour_id=\"" + item.Cells[1].Text + "\" ></Tour>";
            }
        }
        Tour Tour = new Tour();
        Tour.deleteTourByXml(sql);
        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);


    }

    protected void RefreshButton_Click(object sender, EventArgs e)
    {

    }

    protected void Update_Click(object sender, EventArgs e)
    {

        Bitmap bitmap;
        Tour Tour = new Tour();
        Utils utils = new Utils();

        //check nhap so
        List<WebControl> lstWebControl = new List<WebControl>();
        lstWebControl.Add(txt_gia_khuyen_mai);
        lstWebControl.Add(txt_gia_thi_truong);
        lstWebControl.Add(txt_vi_tri);
        lb_messenger.Text = utils.checkDataType(lstWebControl);
        if (lb_messenger.Text == "")
        {

            ObjTour objTour = new ObjTour();
            objTour.Ten_tour = txt_ten_tour.Text;
            objTour.Ma_tour = txt_ma_tour.Text;
            objTour.Mo_ta_ngan = cke_mo_ta_ngan.Text;
            objTour.Gia_thi_truong = int.Parse(txt_gia_thi_truong.Text);
            objTour.Gia_khuyen_mai = int.Parse(txt_gia_khuyen_mai.Text);
            objTour.So_ngay = int.Parse(txt_so_ngay.Text);
            objTour.So_dem = int.Parse(txt_so_dem.Text);

            objTour.So_cho = int.Parse(txt_so_cho.Text);
            objTour.So_cho_da_dat = int.Parse(txt_so_cho_da_dat.Text);
            objTour.Ngay_khoi_hanh = txt_ngay_khoi_hanh.Text;
            objTour.Ngay_ket_thuc = txt_ngay_ket_thuc.Text;

            objTour.Loai_tour_id = int.Parse(drop_danh_muc.SelectedValue);
            objTour.Ma_khuyen_mai = int.Parse(Drop_khuyen_mai.SelectedValue);
            objTour.Vi_tri = int.Parse(txt_vi_tri.Text);
            if (ck_tour_ban_chay.Checked)
            {
                objTour.Tour_ban_chay = 1;
            }
            if (ck_tour_noi_bat.Checked)
            {
                objTour.Tour_noi_bat = 1;
            }
            objTour.Noi_dung = cke_noi_dung.Text;



            //them moi tour
            if (Insert)
            {
                //tao folder neu chua co
                if (!Directory.Exists(MapPath("/images/") + DateTime.Now.ToString("yyyyMM")))
                {
                    Directory.CreateDirectory(MapPath("/images/") + DateTime.Now.ToString("yyyyMM"));
                }
                //check dinh danh anh
                lstWebControl = new List<WebControl>();
                lstWebControl.Add(file_anh_to);
                lstWebControl.Add(file_anh_nho);
                lb_messenger.Text = utils.checkDataType(lstWebControl);
                if (lb_messenger.Text == "")
                {
                    String filename = DateTime.Now.ToString("ddmmyyhhmmss").ToString();
                    bitmap = utils.resizeAnhNho(file_anh_nho);
                    bitmap.Save(MapPath("/images/" + DateTime.Now.ToString("yyyyMM") + "/") + filename + "_1" + Path.GetExtension(file_anh_nho.FileName));// utils.RemoveSign4VietnameseString(file_anh_nho.FileName));
                    //up anh to
                    bitmap = utils.resizeAnhTo(file_anh_to);
                    bitmap.Save(MapPath("/images/" + DateTime.Now.ToString("yyyyMM") + "/") + filename + Path.GetExtension(file_anh_to.FileName));
                    
                    //up anh slide
                    objTour.Anh_slide = "";
                    if (file_slide.HasFile)
                    {
                        bitmap = utils.resizeAnh(file_slide, Constants.ANH_SLIDE_WIDTH, Constants.ANH_SLIDE_HEIGHT);
                        bitmap.Save(MapPath("/img/") + filename + utils.RemoveSign4VietnameseString(file_slide.FileName));

                        objTour.Anh_slide = filename + utils.RemoveSign4VietnameseString(file_slide.FileName);
                    }

                    objTour.Anh_nho = DateTime.Now.ToString("yyyyMM") + "/" + filename + "_1" + Path.GetExtension(file_anh_nho.FileName);
                    objTour.Anh_to = DateTime.Now.ToString("yyyyMM") + "/" + filename + Path.GetExtension(file_anh_to.FileName);
                    Tour.createTour(objTour);
                    Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                }


            }
            //update tour
            else
            {
                objTour.Anh_to = anh_to;
                objTour.Anh_nho = anh_nho;
                objTour.Anh_slide = anh_slide;
                String filename = DateTime.Now.ToString("ddmmyyhhmmss").ToString();
                //neu update anh nho thi xoa anh nho cu di va up lai anh moi
                if (file_anh_nho.HasFile)
                {
                    if (Path.GetExtension(file_anh_nho.FileName).ToUpper() == ".PNG" || Path.GetExtension(file_anh_nho.FileName).ToUpper() == ".JPG")
                    {
                        //xoa anh cu
                        if (anh_nho != "")
                        {

                            if (File.Exists(Server.MapPath("/images/" + anh_nho)))
                                File.Delete(Server.MapPath("/images/" + anh_nho));
                        }
                        anh_nho = "";

                        //up anh moi da thay doi kich thuoc theo chuan 209x165
                        bitmap = utils.resizeAnhNho(file_anh_nho);
                        bitmap.Save(MapPath("/images/" + DateTime.Now.ToString("yyyyMM") + "/") + filename + "_1" + Path.GetExtension(file_anh_nho.FileName));// utils.RemoveSign4VietnameseString(file_anh_nho.FileName));
                        objTour.Anh_nho = DateTime.Now.ToString("yyyyMM") + "/" + filename + "_1" + Path.GetExtension(file_anh_nho.FileName);
                    }
                    else
                    {
                        lb_messenger.Text = "Sai định dạng ảnh";
                        return;
                    }
                }
                //neu update anh to thi xoa anh to cu di va up lai anh moi
                if (file_anh_to.HasFile)
                {

                    if (Path.GetExtension(file_anh_to.FileName).ToUpper() == ".PNG" || Path.GetExtension(file_anh_to.FileName).ToUpper() == ".JPG")
                    {

                        //xoa anh cu
                        if (anh_to != "")
                        {

                            if (File.Exists(Server.MapPath("/images/" + anh_to)))
                                File.Delete(Server.MapPath("/images/" + anh_to));
                        }
                        anh_to = "";

                        //up anh moi da thay doi kich thuoc theo chuan
                        bitmap = utils.resizeAnhTo(file_anh_to);
                        bitmap.Save(MapPath("/images/" + DateTime.Now.ToString("yyyyMM") + "/") + filename + Path.GetExtension(file_anh_to.FileName));

                        objTour.Anh_to = DateTime.Now.ToString("yyyyMM") + "/" + filename + Path.GetExtension(file_anh_to.FileName);
                    }
                    else
                    {
                        lb_messenger.Text = "Sai định dạng ảnh";
                        return;
                    }

                }
                //neu update anh slide thi xoa anh slide cu di va up lai anh moi
                if (ck_slide.Checked == true)
                {
                    objTour.Anh_slide = "";
                    //xoa anh cu
                    if (anh_slide != "")
                    {

                        if (File.Exists(Server.MapPath("/img/" + anh_slide)))
                            File.Delete(Server.MapPath("/img/" + anh_slide));
                    }
                    anh_slide = "";
                }
                else
                {
                    if (file_slide.HasFile)
                    {

                        if (Path.GetExtension(file_slide.FileName).ToUpper() == ".PNG" || Path.GetExtension(file_slide.FileName).ToUpper() == ".JPG")
                        {

                            //xoa anh cu
                            if (anh_slide != "")
                            {

                                if (File.Exists(Server.MapPath("/img/" + anh_slide)))
                                    File.Delete(Server.MapPath("/img/" + anh_slide));
                            }
                            anh_slide = "";

                            //up anh moi da thay doi kich thuoc theo chuan
                            //bitmap = utils.resizeAnh(file_slide, Constants.ANH_SLIDE_WIDTH, Constants.ANH_SLIDE_HEIGHT);
                            file_slide.SaveAs(MapPath("/img/") + filename + utils.RemoveSign4VietnameseString(file_slide.FileName));

                            objTour.Anh_slide = filename + utils.RemoveSign4VietnameseString(file_slide.FileName);
                        }
                        else
                        {
                            lb_messenger.Text = "Sai định dạng ảnh";
                            return;
                        }

                    }
                    
                }
                objTour.Tour_id = int.Parse(Id);
                Tour.updateTour(objTour);
                Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            }
        }
    }

    private void LoadFilterNewsAutocomplete()
    {

    }

    protected void Back_Click(object sender, EventArgs e)
    {
        pnView.Visible = true;
        pnUpdate.Visible = false;
        Level = "";
        Insert = false;
    }

    protected void Filter_Click(object sender, EventArgs e)
    {
        Tour Tour = new Tour();
        List<ObjTour> lst = new List<ObjTour>();
        lst=Tour.getTourBySearch(txtFilterName.Text);
        if (lst.Count > 0)
        {
            grid_san_pham.DataSource = lst;
            grid_san_pham.DataBind();
        }
        else
        {
            lb_messenger.Text = "Không có tour";
            grid_san_pham.DataSource = null;
            grid_san_pham.DataBind();
        }
    }

    protected void UnFilter_Click(object sender, EventArgs e)
    {
        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
    }

    protected void lbtUpdateOrd_Click(object sender, EventArgs e)
    {
        string sql = "";
        foreach (DataGridItem item in grid_san_pham.Items)
        {
            TextBox txt = (TextBox)item.FindControl("txtOrd");
            sql += "<Tour Tour_id=\"" + item.Cells[1].Text + "\" Vi_tri= \"" + txt.Text + "\" ></Tour>";
        }
        Tour Tour = new Tour();
        Tour.updateViTriLoaiTourByXml(sql);
        Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
    }

    protected void lbtUpdateOrdBottom_Click(object sender, EventArgs e)
    {

    }
    protected void Drop_khuyen_mai_select(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String Ma_khuyen_mai = Drop_khuyen_mai.SelectedValue;
            KhuyenMai km = new KhuyenMai();
            km.getKhuyenMaiById(Ma_khuyen_mai);
            ObjKhuyenMai objkm = new ObjKhuyenMai();
            float so_km = objkm.So_khuyen_mai;
            txt_gia_khuyen_mai.Text = so_km.ToString();
        }
       
    }
}
